<?php
//各项目公用函数jetee模式共用
/**
 * 生成本地 合并
 * @param str or array		$js 逗号分隔的
 * @return bool
 */
function js_creat($js) {
	static $mod=null;	if($mod===null) $mod=strpos(C('JS_MOD'),'cdn')!==false ? 'min' : C('JS_MOD');//cdn用min  其它按配置
	$str_js=$js;
	if(is_string($js))
		$js=explode(',',$js);
	else
		$str_js=implode(',',$js);
	$url = 'runtime/'.(APP_DEBUG ? 'debug_':'').md5($str_js).'.js';
	$path = PUBLIC_PATH.$url;
	$url =  DOMAIN_URL.ROOT_URL.PUBLIC_URL.$url;
	//调试模式删除正式模式的
	if(APP_DEBUG && file_exists(PUBLIC_PATH.'runtime/'.md5($str_js).'.js')) @unlink(PUBLIC_PATH.'runtime/'.md5($str_js).'.js') ;
	if(!file_exists($path) || APP_DEBUG){//调试模式覆盖
		$js_content = '';
		foreach($js as $v)
			if($i=js_get($v,$mod,PUBLIC_PATH))
				$js_content .= @file_get_contents($i)."\r\n";
			
		@file_put_contents($path,$js_content);
	}
	return $url.'?v=1';
}
function css_creat($js) {
	static $mod=null;	if($mod===null) $mod=strpos(C('JS_MOD'),'cdn')!==false ? 'min' : C('JS_MOD');//cdn用min  其它按配置
	$str_js=$js;
	if(is_string($js))
		$js=explode(',',$js);
	else
		$str_js=implode(',',$js);
	$url = 'runtime/'.(APP_DEBUG ? 'debug_':'').md5($str_js).'.css';
	$path = PUBLIC_PATH.$url;
	$url =  DOMAIN_URL.ROOT_URL.PUBLIC_URL.$url;
	//调试模式删除正式模式的
	if(APP_DEBUG && file_exists(PUBLIC_PATH.'runtime/'.md5($str_js).'.css')) @unlink(PUBLIC_PATH.'runtime/'.md5($str_js).'.css');
	if(!file_exists($path) || APP_DEBUG){//调试模式覆盖
		make_dir(PUBLIC_PATH.'runtime');
		$js_content = '';
		foreach($js as $v)
			if($i=css_get($v,$mod,PUBLIC_PATH)){
				$js_content .= @file_get_contents($i)."\r\n";
			}
			
		@file_put_contents($path,$js_content);
	}
	return $url.'?v=1';	
}
/**
 * 设置为cdn不合并 
 * @param str or array		$js 逗号分隔的
 * @return bool
 */
function js_creat_cdn($js) {
	static $path=null;	if($path===null) $path=strpos(C('JS_MOD'),'cdn')!==false ? '' : DOMAIN_URL.ROOT_URL.PUBLIC_URL;  
	$str_js=$js;
	if(is_string($js))
		$js=explode(',',$js);
	else
		$str_js=implode(',',$js);
	$return = '';
	foreach($js as $v)
		if($i=js_get($v,C('JS_MOD'),$path))
			$return .= '<script src="'.$i.'"></script>'."\r\n";
	
	return $return;
}
//css不能合并  有路径问题
function css_creat_cdn($js) {
	static $path=null;	if($path===null) $path=strpos(C('JS_MOD'),'cdn')!==false ? '' : DOMAIN_URL.ROOT_URL.PUBLIC_URL;  
	$str_js=$js;
	if(is_string($js))
		$js=explode(',',$js);
	else
		$str_js=implode(',',$js);
	$return = '';
	foreach($js as $v)
		if($i=css_get($v,C('JS_MOD'),$path))
			 $return .= '<link href="'.$i.'" rel="stylesheet">';

	return $return;
}


/**
 *  根据设置 返回js文件  重复取返回false
 *
 * @param str 		$js 要返回的js
 * @param string 	$zip 值为zip压缩   其它不压缩
 * @return 返回js本地或cdn路径   失败false;   
 */
function js_get($js,$mod='',$path='') {
	static $get=array();
	if(isset($get[$js]))
		return false;
	else
		$get[$js]=true;
	$all=array(
		'jquery'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/jquery/1.11.3/jquery.min.js',
			'min'=>'min/jquery/1.12.4/jquery.min.js',
			'no_min'=>'no_min/jquery/1.12.4/jquery.js',
		),	
		'jquery1123'=>array(
			'cdn'=>'https://cdn.staticfile.org/jquery/1.12.3/jquery.min.js',
			'min'=>'min/jquery/1.12.3/jquery.min.js',
			'no_min'=>'no_min/jquery/1.12.3/jquery.js',
		),	
		'jquery214'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js',
			'min'=>'min/jquery/2.1.4/jquery.min.js',
			'no_min'=>'no_min/jquery/2.1.4/jquery.js',
		),
		'jquery224'=>array(
			'cdn'=>'https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js',
			'min'=>'min/jquery/2.2.4/jquery.min.js',
			'no_min'=>'no_min/jquery/2.2.4/jquery.js',
		),	
		'bootstrap'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js',
			'min'=>'min/bootstrap/3.3.7/js/bootstrap.min.js',
			'no_min'=>'no_min/bootstrap/3.3.7/js/bootstrap.js',
		),	
		'artDialog'=>array(
			'cdn'=>'https://cdn.bootcss.com/artDialog/6.0.4/dialog-min.js',
			'min'=>'min/artDialog/6.0.4/dialog-min.js',
			'no_min'=>'no_min/artDialog/6.0.4/dialog.js',
		),	
		'jquery.cookie'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/jquery.cookie/1.4.1/jquery.cookie.min.js',
			'min'=>'min/jquery.cookie/1.4.1/jquery.cookie.min.js',
			'no_min'=>'no_min/jquery.cookie/1.4.1/jquery.cookie.js',
		),	
		'jquery.qrcode'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/jquery-qrcode/1.0.0/jquery.qrcode.min.js',
			'min'=>'min/jquery.qrcode/1.0/jquery.qrcode.min.js',
			'no_min'=>'no_min/jquery.qrcode/1.0/jquery.qrcode.js',
		),	
		'jquery.datetimepicker'=>array(
			'cdn'=>'https://cdn.bootcss.com/jquery-datetimepicker/2.5.4/jquery.datetimepicker.min.js',
			'min'=>'min/jquery.datetimepicker/2.5.4/jquery.datetimepicker.min.js',
			'no_min'=>'no_min/jquery.datetimepicker/2.5.4/jquery.datetimepicker.js',
		),	
		'fancybox'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/fancybox/2.1.5/jquery.fancybox.min.js',
			'min'=>'min/fancybox/2.1.7/jquery.fancybox.min.js',
			'no_min'=>'no_min/fancybox/2.1.7/jquery.fancybox.js',
		),	
		'layui'=>array(
			'cdn'=>DOMAIN_URL.ROOT_URL.PUBLIC_URL.'min/layui/2.5.4/layui.js',
			'min'=>'min/layui/2.5.4/layui.js',
			'no_min'=>'no_min/layui/2.5.4/layui.js',
		),	
		'layui2245'=>array(
			'cdn'=>DOMAIN_URL.ROOT_URL.PUBLIC_URL.'min/layui/2.2.45/layui.js',
			'min'=>'min/layui/2.2.45/layui.js',
			'no_min'=>'no_min/layui/2.2.45/layui.js',
		),	
		'ueditor'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/ueditor/1.4.3.1/ueditor.all.min.js',
			'min'=>'min/ueditor/1.4.3.3/ueditor.all.min.js',
			'no_min'=>'no_min/ueditor/1.4.3.3/ueditor.all.js',
		),
		'html5shiv'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/html5shiv/3.7/html5shiv.min.js',
			'min'=>'min/html5shiv/3.7.3/html5shiv.min.js',
			'no_min'=>'no_min/html5shiv/3.7.3/html5shiv.js',
		),
		'respond'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/respond.js/1.4.2/respond.js',
			'min'=>'min/respond/1.4.2/respond.min.js',
			'no_min'=>'no_min/respond/1.4.2/respond.js',
		),
		'summernote'=>array(
			'cdn'=>'https://cdn.bootcss.com/summernote/0.8.8/summernote.min.js',
			'min'=>'min/summernote/0.8.8/summernote.min.js',
			'no_min'=>'no_min/summernote/0.8.8/summernote.js',
		),
		'webuploader'=>array(
			'cdn'=>'https://cdn.staticfile.org/webuploader/0.1.5/webuploader.js',
			'min'=>'min/webuploader/0.1.5/webuploader.min.js',
			'no_min'=>'no_min/webuploader/0.1.5/webuploader.js',
		),
		'morris'=>array(
			'cdn'=>'https://cdn.bootcss.com/morris.js/0.5.1/morris.min.js',
			'min'=>'min/morris/0.5.1/morris.min.js',
			'no_min'=>'no_min/morris/0.5.1/morris.js',
		),
		'gritter'=>array(
			'cdn'=>DOMAIN_URL.ROOT_URL.PUBLIC_URL.'min/gritter/1.7.4/jquery.gritter.min.js',
			'min'=>'min/gritter/1.7.4/jquery.gritter.min.js',
			'no_min'=>'no_min/gritter/1.7.4/jquery.gritter.js',
		),
		'metisMenu'=>array(
			'cdn'=>'https://cdn.bootcss.com/metisMenu/2.7.1/metisMenu.min.js',
			'min'=>'min/metisMenu/2.7.1/metisMenu.min.js',
			'no_min'=>'no_min/metisMenu/2.7.1/metisMenu.js',
		),
		'slimscroll'=>array(
			'cdn'=>'https://cdn.bootcss.com/jQuery-slimScroll/1.3.8/jquery.slimscroll.min.js',
			'min'=>'min/slimscroll/1.3.8/jquery.slimscroll.min.js',
			'no_min'=>'no_min/slimscroll/1.3.8/jquery.slimscroll.js',
		),
		'layer'=>array(
			'cdn'=>'https://cdn.bootcss.com/layer/2.1/layer.min.js',
			'min'=>'min/layer/2.1/layer.min.js',
			'no_min'=>'no_min/layer/2.1/layer.js',
		),
		'zc_base'=>array(
			'cdn'=>DOMAIN_URL.ROOT_URL.PUBLIC_URL.'zc_base.js',
			'min'=>'zc_base.js',
			'no_min'=>'zc_base.js',
		),	
	);
	return $path.$all[$js][$mod];
}

function css_get($js,$mod='',$path='') {
	static $get=array();
	if(isset($get[$js]))
		return false;
	else
		$get[$js]=true;
	$all=array(
		'bootstrap'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css',
			'min'=>'min/bootstrap/3.3.7/css/bootstrap.min.css',
			'no_min'=>'no_min/bootstrap/3.3.7/css/bootstrap.css',
		),
		'artDialog'=>array(
			'cdn'=>DOMAIN_URL.ROOT_URL.PUBLIC_URL.'min/artDialog/6.0.4/dialog-min.css',
			'min'=>'min/artDialog/6.0.4/dialog-min.css',
			'no_min'=>'no_min/artDialog/6.0.4/dialog.css',
		),	
		'jquery.datetimepicker'=>array(
			'cdn'=>'https://cdn.bootcss.com/jquery-datetimepicker/2.5.4/jquery.datetimepicker.min.css',
			'min'=>'min/jquery.datetimepicker/2.5.4/jquery.datetimepicker.min.css',
			'no_min'=>'no_min/jquery.datetimepicker/2.5.4/jquery.datetimepicker.css',
		),
		'fancybox'=>array(
			'cdn'=>'https://apps.bdimg.com/libs/fancybox/2.1.5/jquery.fancybox.min.css',
			'min'=>'min/fancybox/2.1.7/jquery.fancybox.min.css',
			'no_min'=>'no_min/fancybox/2.1.7/jquery.fancybox.css',
		),	
		'layui'=>array(
			'cdn'=>DOMAIN_URL.ROOT_URL.PUBLIC_URL.'min/layui/2.5.4/css/layui.css',
			'min'=>'min/layui/2.5.4/css/layui.css',
			'no_min'=>'no_min/layui/2.5.4/css/layui.css',
		),	
		'layui2245'=>array(
			'cdn'=>DOMAIN_URL.ROOT_URL.PUBLIC_URL.'min/layui/2.2.45/css/layui.css',
			'min'=>'min/layui/2.2.45/css/layui.css',
			'no_min'=>'no_min/layui/2.2.45/css/layui.css',
		),	
		'summernote'=>array(
			'cdn'=>'https://cdn.bootcss.com/summernote/0.8.8/summernote.css',
			'min'=>'min/summernote/0.8.8/summernote.css',
			'no_min'=>'no_min/summernote/0.8.8/summernote.css',
		),
		'webuploader'=>array(
			'cdn'=>'https://cdn.staticfile.org/webuploader/0.1.5/webuploader.css',
			'min'=>'min/webuploader/0.1.5/webuploader.css',
			'no_min'=>'no_min/webuploader/0.1.5/webuploader.css',
		),
		'font-awesome'=>array(
			'cdn'=>'https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css',
			'min'=>'min/font-awesome/4.7.0/font-awesome.min.css',
			'no_min'=>'no_min/font-awesome/4.7.0/font-awesome.css',
		),
		'animate'=>array(
			'cdn'=>'https://cdn.bootcss.com/animate.css/3.0.0/animate.min.css',
			'min'=>'min/animate/3.0/animate.min.css',
			'no_min'=>'no_min/animate/3.0/animate.css',
		),
		'morris'=>array(
			'cdn'=>'https://cdn.bootcss.com/morris.js/0.5.1/morris.css',
			'min'=>'min/morris/0.5.1/morris.min.css',
			'no_min'=>'no_min/morris/0.5.1/morris.css',
		),
		'gritter'=>array(
			'cdn'=>DOMAIN_URL.ROOT_URL.PUBLIC_URL.'min/gritter/1.7.4/jquery.gritter.css',
			'min'=>'min/gritter/1.7.4/jquery.gritter.css',
			'no_min'=>'no_min/gritter/1.7.4/jquery.gritter.css',
		),
		'metisMenu'=>array(
			'cdn'=>'https://cdn.bootcss.com/metisMenu/1.1.3/metisMenu.min.css',
			'min'=>'min/metismenu/1.1.3/metisMenu.min.css',
			'no_min'=>'no_min/metismenu/1.1.3/metisMenu.css',
		),
		'zc_base'=>array(
			'cdn'=>DOMAIN_URL.ROOT_URL.PUBLIC_URL.'zc_base.css',
			'min'=>'css/zc_base.css',
			'no_min'=>'css/zc_base.css',
		),	
		'global'=>array(
			'cdn'=>DOMAIN_URL.ROOT_URL.PUBLIC_URL.'global.css',
			'min'=>'css/global.css',
			'no_min'=>'css/global.css',
		),	
	);
	return $path.$all[$js][$mod];
}

/**
 * 关键字入库  如果重复记数
 *
 * @param   string      $key        被截取的字符串
 * @return  bool true 新增成功   false 加1
 */
function add_keyword($keyword){
	if(!db('keyword')->add(array('keyword'=>$keyword))){
		db()->query('update '.C('DB_PREFIX').'keyword set num=num+1 where keyword= :keyword',array('keyword'=>$keyword));
		return false;
	}
	return true;
}

